#include "fund.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#include "cJSON.h"


/********************TA函数**********************/
std::string getFullTaName(const Ta * ta) {
	std::string fullName = ta->code + " " + ta->name;
	return fullName;
}
bool isTaBig(const Ta & one, const Ta & two) {
	return (one.code < two.code);
}


/********************PARSE函数**********************/

int getOtcShDate(const Parse * parse) {
	return parse->xy_sh_date + 1;
}
int getOtcFhDate(const Parse * parse) {
	return parse->xy_fh_date + 1;

}
bool isParseBig(const Parse & one, const Parse & two) {

	return (one.index < two.index);
}


/********************Fund函数**********************/

bool isFundEqual(const Fund * one, const Fund * two) {

	return (one->fund_code == two->fund_code);
}


//检查基金名称和TA代码的匹配度
bool fundCheck(const std::string &code, const std::string &name, const std::vector<TaInfo> &info)
{
	//如果TA是98或99则直接反馈true
	if (code == "98" || code == "99")
	{
		return true;
	}

	//循环校验
	for (size_t i = 0; i < info.size(); i++)
	{
		if (name.find(info[i].name) != std::string::npos && info[i].ta != code)
		{			
			//std::cout << "检查不通过" <<std::endl;			
			return false;
		}
		
		if(name.find(info[i].name) != std::string::npos && info[i].ta == code)
		{
			//std::cout << "检查通过" <<std::endl;
			return true;
		}
		
	}	

	return true;
}


//初始化TA和基金名称
void initCheckTaInfo(const std::string &file, std::vector<TaInfo> &info)
{
	//读取文件
	std::ifstream infile;
	infile.open(file,std::ios::in);
	if (!infile.is_open())
	{
		return;
	}
	std::string line;

	//循环导入到TAINFO数组
	while (std::getline(infile, line))
	{
		//如果是已;开头的，直接跳过，为注释
		if(line.substr(0,1)==";" || line.substr(0,1)=="[")
		{
			continue;
		}

		//找到=位置，以=位置为基准去两边数据
		size_t pos = line.find("=");
		if (pos == std::string::npos)
		{
			continue;
		}
		TaInfo temp;
		temp.ta = line.substr(0, pos);
		temp.name = line.substr(pos+1);
	
		info.push_back(temp);
	}

	//关闭文件
	infile.close();
}


void initParse(const std::string & file, std::vector<Parse> & parse)
{
	std::ifstream infile;
	infile.open(file,std::ios::in);
	if(!infile.is_open())
	{
		std::cout << file << " is not fund" <<std::endl;
		return;
	}
	std::string temp;
	std::stringstream ss;

	while (!infile.eof())
	{
		infile >> temp;
		ss << temp;		
	}

	infile.close();
	//std::cout<<ss.str()<<std::endl;	

	cJSON * root = cJSON_Parse(ss.str().c_str());
	if(root==NULL)
	{
		std::cout<<"parse fail" <<std::endl;
		return;
	}
	//printf("%s\n",cJSON_Print(root));

	int pauseSize = cJSON_GetArraySize(root);

	for (int i = 0; i < pauseSize; i++)
	{
		Parse temp;
		cJSON *item = cJSON_GetArrayItem(root, i);
		temp.index = cJSON_GetObjectItem(item, "index")->valueint;
		temp.parse_id = cJSON_GetObjectItem(item, "id")->valuestring;
		temp.parse_name = cJSON_GetObjectItem(item, "name")->valuestring;
		temp.xy_fh_date = cJSON_GetObjectItem(item, "fh")->valueint;
		temp.xy_sh_date = cJSON_GetObjectItem(item, "sh")->valueint;
		
		parse.push_back(temp);
	}

	//对基金参数进行排序
	std::sort(parse.begin(), parse.end(), isParseBig);
}

void initTaInfo(const std::string & file, std::vector<Ta> & info )
{
	//读取文件
	std::ifstream infile;
	infile.open(file,std::ios::in);
	if (!infile.is_open())
	{
		return;
	}
	std::string line;

	//循环导入到TAINFO数组
	while (std::getline(infile, line))
	{
		//如果是已;开头的，直接跳过，为注释
		if(line.substr(0,1)==";" || line.substr(0,1)=="[")
		{
			continue;
		}

		//找到=位置，以=位置为基准去两边数据
		size_t pos = line.find("=");
		if (pos == std::string::npos)
		{
			continue;
		}
		Ta temp;
		temp.code = line.substr(0, pos);
		temp.name = line.substr(pos+1);
	
		info.push_back(temp);
	}

	//关闭文件
	infile.close();

	//对TA进行排序
	std::sort(info.begin(),info.end(),[=](const Ta & a,const Ta & b){return a.code < b.code;});
	
}